define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Adddiscount, GoPage, GoDialog, $stateParams, $ionicModal, Auth, $timeout) {
		$scope.input = new Object();
		$scope.input.id = ""; //优惠券ID
		$scope.input.amount = ""; //优惠券面额
		$scope.input.amount2 = "0.00"; //显示
		$scope.input.name = ""; //优惠券名称
		$scope.input.rule = ""; //优惠券规则
		$scope.input.status = ""; //优惠券状态
		$scope.input.status2 = true; //显示用的状态
		$scope.input.cond1 = false; //使用条件模式1
		$scope.input.cond2 = false; //使用条件模式2
		$scope.input.condition = ""; //提交的使用条件
		$scope.input.condition_amount = ""; //提交的满额减
		$scope.input.condition_amount1 = ""; //用户输入的
		$scope.input.start_time = null; //优惠券开始时间
		$scope.input.end_time = null; //优惠券结束时间
		$scope.input.issue_count = ""; //优惠券发放数量
		$scope.input.limit_count = ""; //优惠券限领数量
		$scope.input.level = new Array(); //提交的用户等级
		$scope.level = [false, false, false]; //显示用的用户等级
		$scope.store_id = "";
		$scope.count = 0;
		$scope.input.glist = new Array(); //要提交的
		$scope.input.key = "";
		$scope.input.goods = new Array(); //显示用，不提交
		$scope.issue_count = 0;
		$scope.input.type = $stateParams.type; //0是平台 1不是
		//初始化面额		
		$scope.moneys = [{
			money: 3
		}, {
			money: 5
		}, {
			money: 10
		}, {
			money: 20
		}, {
			money: 50
		}, {
			money: 100
		}, {
			money: 500
		}, {
			money: 1000
		}];

		if($scope.input.type != 0) {
			if($stateParams.id) {
				$scope.input.id = $stateParams.id;
				$scope.title = "编辑优惠劵";
			} else {
				$scope.title = "添加优惠劵";
			}
		} else {
			$scope.input.id = $stateParams.id;
			$scope.title = "优惠劵详情";
		}

		//监听面额输入
		$scope.$watch("input.amount", function(n, o) {
			var a2 = parseInt(n) + 0.01;
			if(!isNaN(a2)) {
				$scope.input.amount2 = a2;
			} else {
				$scope.input.amount2 = 0;
			}
		})

		//初始化店铺商品
		$scope.init_goodsList = function() {
			Adddiscount.goodslist($scope.store_id, $scope.input.key, 1, function(data) {
				if(data != null) {
					//console.log(data)
					$scope.goods = data.goods; //店铺所有商品
					$scope.count = data.page.count; //商品总数
				} else {
					$scope.goods = new Array();
					$scope.count = 0;
				}
			});
		}

		//初始化
		$scope.init_data = function() {			
			var lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) {
				lan = 20;
			}
			var all = window.innerHeight;
			$scope.myObj = {
				"height": (all - lan - 85) + "px"
			}						
			$scope.store_id = Auth.get_store_id(); //店铺id
			//是平台 不检索商品
			if($scope.input.type != 0) {
				$scope.init_goodsList();
			}
			if($stateParams.id) { //如果有id，说明是编辑
				Adddiscount.getInfo($scope.input.id, function(data) {
					//console.log(data)
					if(!data) {
						return;
					}
					$scope.input.id = data.id;
					$scope.input.amount = data.amount;
					$scope.input.name = data.name;
					$scope.input.rule = data.rule;
					$scope.input.status = data.status;
					if($scope.input.status == 1) {
						$scope.input.status2 = false;
					} else {
						$scope.input.status2 = true;
					}
					$scope.input.condition = data.condition;
					if($scope.input.condition == 1) {
						$scope.input.cond1 = true;
						$scope.input.cond2 = false;
						$scope.input.condition_amount1 = data.condition_amount;
					} else {
						$scope.input.cond1 = false;
						$scope.input.cond2 = true;
						$scope.input.condition_amount2 = data.condition_amount;
					}
					$scope.input.start_time = new Date(data.start_time * 1000);
					$scope.input.end_time = new Date(data.end_time * 1000);
					$scope.input.issue_count = data.issue_count;
					$scope.issue_count = data.issue_count;
					$scope.input.limit_count = data.limit_count;
					$scope.input.level = data.level;
					$scope.input.goods = data.goods;
					$scope.bind_level();
				});
			}
		}
		$scope.init_data();

		$scope.bind_level = function() {
			for(var i = 0; i < $scope.input.level.length; i++) {
				$scope.level[$scope.input.level[i] - 1] = true;
			}
		}

		//选择推送对象和方式
		$scope.select_level = function() {
			$scope.input.level = new Array();
			for(var k = 0; k < $scope.level.length; k++) {
				if($scope.level[k]) {
					$scope.input.level.push(parseInt(k) + 1);
				}
			}
		}

		//使用条件：满多少元可使用
		$scope.u_cond1 = function() {
			$scope.input.cond1 = true;
			$scope.input.cond2 = false;
			$scope.input.condition = 1;
		}
		$scope.u_cond2 = function() {
			$scope.input.cond1 = false;
			$scope.input.cond2 = true;
			$scope.input.condition = 2;
		}

		//判断必填项
		/**
		 * 	$scope.input.id,
					$scope.input.name,
					$scope.input.rule,
					$scope.input.amount,
					$scope.input.condition,
					$scope.input.condition_amount,
					$scope.input.issue_count,
					$scope.input.limit_count,
					$scope.input.start_time,
					$scope.input.end_time,
					$scope.input.level,
					$scope.input.status,
					$scope.input.glist,
		 */
		$scope.flag = true;
		$scope.update_data = function() {
			$scope.input.condition_amount = 0;
			if(!$scope.input.name) {
				GoDialog.toast("请输入优惠券名称！");
				$scope.flag = false;
				return;
			} else if(!$scope.input.amount) {
				GoDialog.toast("请选择优惠券面额！");
				$scope.flag = false;
				return;
			} else if(!$scope.input.condition) {
				GoDialog.toast("请勾选使用条件！");
				$scope.flag = false;
				return;
			} else if(!$scope.input.start_time) {
				GoDialog.toast("请选择开始时间！");
				$scope.flag = false;
				return;
			} else if(!$scope.input.end_time) {
				GoDialog.toast("请选择结束时间！");
				$scope.flag = false;
				return;
			} else if(!$scope.input.issue_count) {
				GoDialog.toast("请输入优惠券发行量！");
				$scope.flag = false;
				return;
			} else if(!$scope.input.limit_count) {
				GoDialog.toast("请选择优惠券限领量！");
			} else {
				$scope.select_level(); //会员等级
				if($scope.input.level.length == 0) {
					GoDialog.toast("请选择会员等级！");
					$scope.flag = false;
					return;
				}
			}

			if($scope.input.condition == 1) {
				if($scope.input.condition_amount1) {
					$scope.input.condition_amount = $scope.input.condition_amount1;
				} else {
					GoDialog.toast("请输入满额减金额！");
					$scope.flag = false;
					return;
				}
			} else if($scope.input.condition == 2) {
				$scope.input.condition_amount = parseInt($scope.input.amount) + 0.01;
			}
			$scope.input.start_time = new Date($scope.input.start_time).format("yyyy-MM-dd");
			$scope.input.end_time = new Date($scope.input.end_time).format("yyyy-MM-dd");
			if($scope.input.status2) { //启用状态
				$scope.input.status = 1;
			} else {
				$scope.input.status = 2;
			}
			if($stateParams.id) {
				if($scope.issue_count > $scope.input.issue_count) {
					GoDialog.toast("发行量只能增加！");
					$scope.flag = false;
					return;
				}
			}
			$scope.flag = true;
		}
		//找出哪些商品是已经被选中的(修改的时候才会用到该方法)
		$scope.findSelectedGoods = function() {
			for(var i = 0; i < $scope.goods.length; i++) {
				for(var j = 0; j < $scope.input.goods.length; j++) {
					if($scope.goods[i].goods_id == $scope.input.goods[j].goods_id) {
						$scope.goods[i].isSelected = true;
						break;
					}
				}
			}
		}
		//选中了哪些商品
		$scope.getSelectedGoods = function() {
			$scope.input.goods.splice(0, $scope.input.goods.length); //先清空
			$scope.input.glist.splice(0, $scope.input.glist.length); //先清空
			if($scope.goods != undefined || $scope.goods != null) {
				for(var i = 0; i < $scope.goods.length; i++) {
					if($scope.goods[i].isSelected) { //如果选中了，就会有isSelected这个属性
						$scope.input.glist.push($scope.goods[i].goods_id);
						$scope.input.goods.push($scope.goods[i]);
					}
				}
			}
		}
		//单选
		$scope.selectgoods = function(index) {
			$scope.goods[index].isSelected = !$scope.goods[index].isSelected;
		}
		//全选
		$scope.selectAll = function() {
			$scope.slctall = !$scope.slctall;
			for(var i = 0; i < $scope.goods.length; i++) {
				if($scope.goods[i].isSelected) { //如果选中了，就会有isSelected这个属性
					$scope.goods[i].isSelected = false;
				} else {
					$scope.goods[i].isSelected = true;
				}
			}
		}

		$ionicModal.fromTemplateUrl("templates/modal/adddiscount_goodslist.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal = modal;
		});
		//打开
		$scope.openModal_goodsList = function() {
			$scope.modal.show();
			if($stateParams.id) {
				$scope.findSelectedGoods();
			}
		}
		//隐藏，并返回选择的商品
		$scope.closeModal_goodsList = function() {
			$scope.modal.hide();
			$scope.getSelectedGoods();
		}
		//单纯隐藏
		$scope.closeModal = function() {
			$scope.modal.hide();
		}
		/***************选择商品上下拉刷新*******************/
		$scope.isLoad = false; //可以加载
		$scope.input.nPage = 1;
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Adddiscount.goodslist($scope.store_id, $scope.input.key, 1, function(data) {
					if(data != null) {
						$scope.slctall = false;
						$scope.goods = data.goods;
						$scope.count = data.page.count; //商品总数
					} else {
						$scope.goods = new Array();
						$scope.count = 0; //商品总数
					}
					$scope.input.nPage = 1;
				},
				function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			Adddiscount.goodslist($scope.store_id, $scope.input.key, $scope.input.nPage + 1, function(data) {
					if(data != null && data.goods != null) {
						if($scope.goods != undefined) {
							$scope.goods = $scope.goods.concat(data.goods);
							$scope.input.nPage += 1;
						} else {
							$scope.goods = data.goods;
						}
						$scope.count = data.page.count; //商品总数
					}
				},
				function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
		};
		/***************选择商品上下拉刷新*******************/

		//控制显示更多已经选中的商品
		$scope.isShowMore = true;
		$scope.showMore = function() {
			$scope.isShowMore = !$scope.isShowMore;
		}

		$scope.submit = function() {
			$scope.update_data();
			if($scope.flag) {
				Adddiscount.add(
					$scope.input.id,
					$scope.input.name,
					$scope.input.rule,
					$scope.input.amount,
					$scope.input.condition,
					$scope.input.condition_amount,
					$scope.input.issue_count,
					$scope.input.limit_count,
					$scope.input.start_time,
					$scope.input.end_time,
					$scope.input.level,
					$scope.input.status,
					$scope.input.glist,
					function(data) {
						if(data.success == true) {
							GoDialog.toast("优惠券发布成功！");
							GoPage.go_states("issuediscount");
						} else {
							GoDialog.toast(data.msg);
						}
					}
				)
			}
		}

		//选择优惠方案
		$ionicModal.fromTemplateUrl("templates/modal/moneyselect.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modalmoney = modal;
		});
		//打开
		$scope.openModal_tag = function(solutions_checked) {
			for(var i = 0; i < $scope.moneys.length; i++) {
				if($scope.moneys[i].money == $scope.input.amount) {
					$scope.moneys[i].isSelected = true;
				} else {
					$scope.moneys[i].isSelected = false;
				}
			}
			$scope.modalmoney.show();
		}
		//隐藏
		$scope.closeModal_tag = function() {
			$scope.modalmoney.hide();
		}
		//暂时
		$scope.selectTagId = function(index) {
			for(var i = 0; i < $scope.moneys.length; i++) {
				$scope.moneys[i].isSelected = false;
			}
			$scope.moneys[index].isSelected = true;
		}
		//修改面额
		$scope.updatarole = function() {
			for(var i = 0; i < $scope.moneys.length; i++) {
				if($scope.moneys[i].isSelected) {
					$scope.input.amount = $scope.moneys[i].money;
				}
			}
			$scope.closeModal_tag();
		}
		//提示
		$scope.show_i = function() {
			GoDialog.alert("提示", "修改发行量时只能增加不能减少，请谨慎设置");
		}
		$scope.show_maxi = function() {
			GoDialog.alert("提示", "请结合客单价谨慎设置无门槛，误设、错设等风险由您自负");
		}
		//选择会员等级
		$scope.updatalevel = function(type) {
			$scope.level[type] = !$scope.level[type];
		}
		//删除选中的商品
		$scope.delgoods = function(index) {
			for(var i in $scope.goods) {
				if($scope.goods[i].goods_id == $scope.input.goods[index].goods_id) {
					$scope.goods[i].isSelected = false;
				}
			}
			$scope.slctall = false;
			$scope.input.goods.splice(index, 1);

		}

	}

	controller.$inject = ['$scope', 'Adddiscount', 'GoPage', 'GoDialog', '$stateParams', '$ionicModal', 'Auth', '$timeout'];

	return app.registerController("adddiscountctrl", controller);
});